package org.linuxsogood.boot.queue;

import lombok.Data;

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

/**
 * @author liu.hongwei
 * @since 2020/4/27 15:17
 */
@Data
public class Message implements Delayed {

    private Action msg;

    private long time;

    public Message(Action msg, long time, TimeUnit unit) {
        this.msg = msg;
        this.time = System.currentTimeMillis() + (time > 0 ? unit.toMillis(time) : 0);
    }

    @Override
    public long getDelay(TimeUnit unit) {
        return time - System.currentTimeMillis();
    }

    @Override
    public int compareTo(Delayed o) {
        Message msg = (Message) o;
        return getTime() - msg.getTime() >= 0 ? 1 : -1;
    }
}
